package com.qsxc.utils;

import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Description:
 *
 * @Author:
 * @since: 2021-06-09
 */
public class DictionaryUtil {
    private static HashMap<String, List<KeyValueBean>> distionaryList;

    static {
        distionaryList = new HashMap<>();
        AddDistionary("sm_system_eval_item.type", "1:课程内容;2:教学方法;3:教学效果");
        AddDistionary("sm_studnet.cardtype", "1:身份证;2:护照; 3:军官证;4:其他");
    }

    /**
     * 通过字典值获取对应的字典名称
     *
     * @param distionaryName
     * @param value
     * @return
     */
    public static String getNameByValue(String distionaryName, String value) {
        if (StringUtil.isEmpty(value)) {
            return null;
        }
        List<KeyValueBean> list = distionaryList.get(distionaryName);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        } else {
            for (KeyValueBean item : list) {
                if (item.getValue().equals(value)) {
                    return item.getName();
                }
            }
        }
        return null;
    }

    /**
     * 通过字典值获取对应的字典名称
     *
     * @param distionaryName
     * @param value
     * @return
     */
    public static String getNameByValueDefault(String distionaryName, String value, String defaultValue) {
        if (StringUtil.isEmpty(value)) {
            return defaultValue;
        }
        List<KeyValueBean> list = distionaryList.get(distionaryName);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        } else {
            for (KeyValueBean item : list) {
                if (item.getValue().equals(value)) {
                    return item.getName();
                }
            }
        }
        return null;
    }

    /**
     * 通过字典名称获取值
     *
     * @param distionaryName
     * @param name
     * @return
     */
    public static String getValueByName(String distionaryName, String name) {
        if (StringUtil.isEmpty(name)) {
            return null;
        }
        List<KeyValueBean> list = distionaryList.get(distionaryName);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        } else {
            for (KeyValueBean item : list) {
                if (item.getName().equals(name)) {
                    return item.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 保存字典配置信息
     *
     * @param keyName
     * @param values
     */
    private static void AddDistionary(String keyName, String values) {
        ArrayList<KeyValueBean> list = new ArrayList<>();
        String[] rows = values.split(";");
        for (String item : rows) {
            String[] cols = item.split(":");
            if (cols.length >= 2) {
                list.add(KeyValueBean.builder().value(cols[0].trim()).name(cols[1].trim()).build());
            }
        }
        distionaryList.put(keyName, list);
    }

    public static ArrayList<KeyValueBean> getKeyValueList(String values) {
        if (StringUtil.isEmpty(values)) {
            return null;
        }
        ArrayList<KeyValueBean> list = new ArrayList<>();
        String[] rows = values.split(";");
        for (String item : rows) {
            String[] cols = item.split(":");
            if (cols.length >= 2) {
                list.add(KeyValueBean.builder().value(cols[0].trim()).name(cols[1].trim()).build());
            }
        }
        return list;
    }

    public static String getNameByValueDefault(ArrayList<KeyValueBean> list, String value, String defaultValue) {
        if (CollectionUtil.isEmpty(list)) {
            return defaultValue;
        }
        for (KeyValueBean item : list) {
            if (item.getValue().equals(value)) {
                return item.getName();
            }
        }
        return defaultValue;
    }

    @Data
    @Builder
    public static class KeyValueBean {
        private String name;
        private String value;
    }
}
